import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;

public class CourseOperation extends JFrame implements ActionListener
{
	//Singleton
	private static CourseOperation co=null;
	
	JButton  btnAddCourse,btnExit;
	JLabel lblHeading,lblCouseCode,lblCouseName,lblCourseType,lblCredits,lblHasPractical;
	JTextField txtCouseCode,txtCouseName,txtCredits;
	JRadioButton rbtnCore,rbtnOptional,rbtnProject,rbtnYes,rbtnNo;
	
	private CourseOperation()
	{
		//Outer Panel
		JPanel screenMain= (JPanel)getContentPane( );
		screenMain.setLayout( new BorderLayout() );		
		screenMain.setBorder(new EmptyBorder(10, 10, 10, 10));
		//
		//Inner Panel
		JPanel screenSub=new JPanel( );
		screenSub.setLayout( new GridLayout( 6,3,8,8 ));		
		//
		
		//Labels
		//Heading
		lblHeading=new JLabel(ProgramUitl.ADD_COURSE_TEXT);
		Font fontHeading=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.HEADING_FONT_STLYE,
									ProgramUitl.HEADING_FONT_SIZE);
		lblHeading.setFont(fontHeading);
		screenMain.add("North",lblHeading);		
		//
		Font fontLbl=new Font(ProgramUitl.FONT_NAME,
									ProgramUitl.NORMAL_FONT_STLYE,
									ProgramUitl.NORMAL_FONT_SIZE);
		
		lblCouseCode=new JLabel(ProgramUitl.COURSE_CODE_TEXT);						
		lblCouseCode.setFont(fontLbl);
		
		lblCredits=new JLabel(ProgramUitl.COURSE_CREDITS_TEXT);						
		lblCredits.setFont(fontLbl);			
		
		lblCouseName=new JLabel(ProgramUitl.COURSE_NAME_TEXT);						
		lblCouseName.setFont(fontLbl);	
		
		lblCourseType=new JLabel(ProgramUitl.COURSE_TYPE_TEXT);						
		lblCourseType.setFont(fontLbl);			
		
		lblHasPractical=new JLabel(ProgramUitl.COURSE_PRACTICAL);						
		lblHasPractical.setFont(fontLbl);			
		//
		
		//Text Boxes
		//txtStudentNo=new JTextField();
		txtCouseCode=new JTextField();
		txtCouseName=new JTextField();
		txtCredits=new JTextField();
		//
		
		//Buttons
		btnAddCourse=new JButton(ProgramUitl.ADD_TEXT);
		btnExit=new JButton(ProgramUitl.EXIT_TEXT,ProgramUitl.getButtonIcon(ProgramUitl.ICON_TYPE.ERROR));
		
		btnAddCourse.addActionListener(this);
		btnExit.addActionListener(this);
		//
		
		//Radio Buttons
		ButtonGroup bgCourseType = new ButtonGroup( );
		rbtnCore = new JRadioButton(ProgramUitl.COURSE_TYPE__CORE_TEXT);
		rbtnOptional = new JRadioButton(ProgramUitl.COURSE_TYPE__OPTIONAL_TEXT);
		rbtnProject = new JRadioButton(ProgramUitl.COURSE_TYPE__PROJECT_TEXT);
		bgCourseType.add(rbtnCore);
		bgCourseType.add(rbtnOptional);
		bgCourseType.add(rbtnProject);
						
		ButtonGroup bgHasPracticals = new ButtonGroup( );
		rbtnYes = new JRadioButton(ProgramUitl.YES_TEXT);
		rbtnNo = new JRadioButton(ProgramUitl.NO_TEXT);
		bgHasPracticals.add(rbtnYes);
		bgHasPracticals.add(rbtnNo);		
		//
		
		//Layout Design
		
		screenSub.add(lblCouseCode);
		screenSub.add(txtCouseCode);
		
		screenSub.add(lblCouseName);
		screenSub.add(txtCouseName);
		
		screenSub.add(lblCredits);
		screenSub.add(txtCredits);
		
		screenSub.add(lblCourseType);
		
		JPanel jpCt=new JPanel(new FlowLayout(FlowLayout.LEFT));
		jpCt.add(rbtnCore);
		jpCt.add(rbtnOptional);	
		jpCt.add(rbtnProject);	
		rbtnCore.setSelected(true);
		screenSub.add(jpCt);
						
		screenSub.add(lblHasPractical);
		
		JPanel jpHp=new JPanel(new FlowLayout(FlowLayout.LEFT));		
		jpHp.add(rbtnYes);
		jpHp.add(rbtnNo);
		rbtnYes.setSelected(true);
		screenSub.add(jpHp);		
		
		screenSub.add(btnAddCourse);		
		screenSub.add(btnExit);		
		//
		
		//Inner panel added to Outer Panel 
		screenMain.add("Center",screenSub);
		//
		//Frame Settings
		addWindowListener(new WindowListener() {			
			public void windowClosing(WindowEvent e){}
			public void windowOpened(WindowEvent e) 
			{
				MainMenu.getInstance().setVisible(false);
				MainMenu.getInstance().setEnabled(false);
			}			       
			public void windowClosed(WindowEvent e) {}
			public void windowIconified(WindowEvent e) {}
			public void windowDeiconified(WindowEvent e) {}
			public void windowActivated(WindowEvent e) {}
			public void windowDeactivated(WindowEvent e) {}
		});


		
		setSize(ProgramUitl.SMALL_SCREEN_WIDTH_2,ProgramUitl.SMALL_SCREEN_HEIGHT);
		setLocation(ProgramUitl.SCREEN_LOCATION_TOP,ProgramUitl.SCREEN_LOCATION_LEFT);
		setLocationRelativeTo(null);
		setTitle(ProgramUitl.HEADNING_TEXT);		
        setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);		
		setUndecorated(true);		
		setVisible(true);
		
		validate();
		//
		
	}
	
	public static CourseOperation getInstance()
	{
		if(co==null)
		{
			co=new CourseOperation();
		}
		
		return co;
	}
	//
	
	//Method
	boolean Confirmation(String txt)
	{
		int confirmed = JOptionPane.showConfirmDialog(null, 
				txt, ProgramUitl.ADD_COURSE_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		return confirmed == JOptionPane.YES_OPTION;
	}	
	public void add()
	{
		if(isValidInputs())
		{
			if(Confirmation(ProgramUitl.WISH_TO_PROCEED))
			{
				Course c=new Course();
				
				c.setCode(Integer.parseInt(txtCouseCode.getText().trim()));
				c.setName(txtCouseName.getText().trim());
				c.setCredits(Integer.parseInt(txtCredits.getText().trim()));

				if(rbtnCore.isSelected()==true)
				{
					c.setType(ProgramUitl.COURSE_TYPE.CORE);
				}
				else if(rbtnOptional.isSelected()==true)
				{
					c.setType(ProgramUitl.COURSE_TYPE.OPTIONAL);
				}
				else
				{
					c.setType(ProgramUitl.COURSE_TYPE.PROJECT);
				}
				
				if(rbtnYes.isSelected()==true)
				{
					c.setHasPracticals(true);			
				}
				else
				{
					c.setHasPracticals(false);			
				}			
				c.setStatus(ProgramUitl.STATUS.ENABLED);
				
				try
				{
					ProgramUitl.ERROR_TYPE rt=XmlOperations.getInstance().addCourse(c);
					
					switch(rt)
					{
						case ADDED:
							{					
								JOptionPane.showMessageDialog(null,c.getName() + ProgramUitl.ADDED_SUCCESS_TEXT,ProgramUitl.ADD_COURSE_TEXT,JOptionPane.INFORMATION_MESSAGE);
								clearText();						
							}
							break;
						case ALREADY_EXISTS:
							JOptionPane.showMessageDialog(null,txtCouseCode.getText().trim() + ProgramUitl.ALREADY_EXISTS_TEXT,ProgramUitl.ADD_COURSE_TEXT,JOptionPane.WARNING_MESSAGE);
							break;
						default:
							break;
					}
				}
				catch(Exception ex)
				{
					JOptionPane.showMessageDialog(null,ex.getMessage(),ProgramUitl.ADD_COURSE_TEXT,JOptionPane.ERROR_MESSAGE);	
				}
			}
		}
	}
	
	public boolean isValidInputs()
	{
		String msg="";
		
		//Code
		if(ProgramUitl.IsEmptyString(txtCouseCode.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblCouseCode.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.isPositiveNumeric(txtCouseCode.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblCouseCode.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		
		//Name
		if(ProgramUitl.IsEmptyString(txtCouseName.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblCouseName.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}		
		
		//Credits
		if(ProgramUitl.IsEmptyString(txtCredits.getText().trim()))
		{
			msg+=ProgramUitl.buildMessageItem(lblCredits.getText().trim() + ProgramUitl.REQUIRED_FIELD_ERROR);
		}
		else if(ProgramUitl.isPositiveNumeric(txtCredits.getText().trim())==false)
		{
			msg+=ProgramUitl.buildMessageItem(lblCredits.getText().trim() + ProgramUitl.INVALID_FORMAT_ERROR);
		}
		
		if(ProgramUitl.IsEmptyString(msg)!=true)
		{
			JOptionPane.showMessageDialog(null,msg,ProgramUitl.ADD_COURSE_TEXT,JOptionPane.ERROR_MESSAGE);
		}
		
		//Creadits
		return ProgramUitl.IsEmptyString(msg);
	}
	
	public void clearText()
	{		
		txtCouseCode.setText(null);
		txtCouseName.setText(null);	
		txtCredits.setText(null);	

		rbtnCore.setSelected(true);
		rbtnYes.setSelected(true);
		
		txtCouseCode.requestFocus();		
	}
	
	void exit() 
	{ 		
		int confirmed = JOptionPane.showConfirmDialog(null, 
				ProgramUitl.EXIT_PRORAM_MSG, ProgramUitl.ADD_COURSE_TEXT, 
				JOptionPane.YES_NO_OPTION); 
				
		if (confirmed == JOptionPane.YES_OPTION) 
		{        
				MainMenu.getInstance().setEnabled(true);
				MainMenu.getInstance().setVisible(true);
				co=null;
				this.dispose();
		} 
	}	
	//
	
	//Events
	public void actionPerformed(ActionEvent ae)
	{
		if(ae.getSource().equals(btnExit))
		{
			exit();
		}	
		else if(ae.getSource().equals(btnAddCourse))
		{			
			add();			
		}		
	}	
	//
}